<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Builder;

class App extends Model
{
	protected $guarded = [];
	protected $table = 'apps';
	protected $primaryKey = 'name';
	protected $keyType = 'string';
	public $incrementing = false;
	public $casts = ['sets'=>'json','def_sets'=>'json'];  //设置字段格式 



	/**
	 * 为路由模型获取键名。
	 *
	 * @return string
	*/ 
	public function getRouteKeyName()
	{
		return 'name';
	}

    protected static function boot() {    
        parent::boot();
		static::addGlobalScope('avaiable',function(Builder $builder){
			$builder->where("status",1);
		});
		/*
        static::deleting(function($model) {
			if($model->forceDeleting){
				$model->cates()->get(['app_name'])->each->delete();
			}
        });

        static::deleted(function ($model) {
            $model->removeAppsCache();
        });*/
    }


	public function cates()
	{
		return $this->hasMany(Cate::class,'app_name','name');
	}


	public function removeCache()
    {
        \Cache::forget('sys_table_apps');
    }


	//排序，按主-依赖的方式
	protected function set_order()
	{
		$lists = $this->where('created_at',0)->get()->toArray();
		/*
		$rs = [];
		$order = 0;
		foreach($lists as $app){
			$order ++;
			$rs[$app['id']] = $order;
			if($app['childs']){
				foreach($app['childs'] as $sub){
					$order ++;
					$rs[$sub['id']] = $order;
				}		
			}
		}*/
		return $lists;
	}

	public function getTitleAttribute($value){
		if(!empty($this->attributes['sets'])){
			$sets = json_decode($this->attributes['sets'],true);
			if(is_array($sets) && !empty($sets['app']['title']) ){
				$value = $sets['app']['title'];
			}
		}
		return $value;
	}
	//用于恢复默认值
	public function getOldSetsAttribute($value){
		if(!empty($this->attributes['sets'])){
			$value = json_decode($this->attributes['sets'],true);
		}else{
			$value = [];
		}
		return $value;
	}
	public function getSetsAttribute($value){
		$sets = json_decode($this->attributes['sets'],true);
		
		$tabs = [];   //实际启用的模块 判断aa是否启用,if($tabs['aa'])即可
		foreach(str2arr($this->attributes['exts']) as $v){
			$tabs[$v] = 1;			
		}
		foreach(str2arr($this->attributes['commons']) as $v){
			$tabs[$v] = 0;
		}

		$sets['base'] = $sets['base'] ?? [];
		$tabs = array_merge($tabs,array_filter($sets['base'])); //将启用的公共模块添加入tabs
		foreach($tabs as $k=>$v){
			if($v){   //开启
				if(empty($sets[$k])){
					$sets[$k] = config('exts.'.$k.'.sets') ?? [];
				}
			}else{   //开未启
				$sets[$k] = [];
			}
		}	
		
		if(empty($sets['app'])) $sets['app'] = json_decode($this->attributes['def_sets'],true)??[];  //$sets['app']补充
		$sets['tabs'] = $tabs;
		//无指定分类 || 分类未启用 || 分类设置不启用
		if( empty($sets['tabs']['cate']) || empty($sets['cate']['set']) ){
			$sets['has_cate_sets'] = false;  //不允许分类设置
		}else{
			$sets['has_cate_sets'] = true;   //允许分类设置
		}
		
		return $sets;
	}

	public function getExtsAttribute($value){
		return str2arr($value);
	}
	public function getCommonsAttribute($value){
		return str2arr($value);
	}
}
